/*
 * Camara.cpp
 *
 *  Created on: Sep 30, 2013
 *      Author: matias
 */

#include "Camara.h"
#include <glm/gtc/matrix_transform.hpp>

Camara::Camara(){
	//Al no recibir los valores la pongo en una posicion por defecto.
	this->posicion = new glm::vec3(1.0f, 0.0f, 0.0f);
	this->objetivo = new glm::vec3(0.0f, 0.0f, 0.0f);
	this->normal = new glm::vec3(0.0f, 0.0f, 1.0f);
	this->rotacionY = 45;
	this->rotacionZ = 0;
	this->zoom = 500;
	this->modoEstatico = false;
}

Camara::~Camara(){
	delete this->posicion;
	delete this->objetivo;
	delete this->normal;
}

glm::mat4* Camara::getViewMatrix(){
	glm::vec3 pos = glm::vec3((*this->posicion).x + this->zoom, (*this->posicion).y, (*this->posicion).z);
	glm::mat4 view_matrix = glm::lookAt(pos , *this->objetivo, *this->normal);
	view_matrix = glm::rotate(view_matrix, this->rotacionY, glm::vec3(0.0, 1.0, 0.0));
	view_matrix = glm::rotate(view_matrix, this->rotacionZ, glm::vec3(0.0, 0.0, -1.0));
	return new glm::mat4(view_matrix);
}

void Camara::bindViewCamara(GLuint handle){
	glm::mat4* view_matrix = this->getViewMatrix();
	GLuint location_view_matrix = glGetUniformLocation( handle, "ViewMatrix");
	if( location_view_matrix >= 0 )
	{
		glUniformMatrix4fv( location_view_matrix, 1, GL_FALSE, &((*view_matrix)[0][0]));
	}
	delete view_matrix;
}

void Camara::rotarCamara(int y, int z){
	if (!modoEstatico){
	this->rotacionY += y;
	if (this->rotacionY > 90) this->rotacionY = 90;
	if (this->rotacionY < 0) this->rotacionY = 0;
	this->rotacionZ += z;
	}
}

void Camara::zoomIn(){
	//Modificar la posicion de la camara.
	if (!modoEstatico){
	this->zoom = this->zoom - 3;
	if (this->zoom < 0) this->zoom = 0;}
}

void Camara::zoomOut(){
	//Modificar la posicion de la camara.
	if (!modoEstatico)
	this->zoom++;
}

void Camara::rotarCamaraEstatica() {
	if (modoEstatico)
	this->rotacionZ++;
}

void Camara::cambiarModo() {
	this->modoEstatico = !(this->modoEstatico);
}
